<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Golang字符串 | J.のblog</title><meta name="keywords" content="云原生,golang"><meta name="author" content="J."><meta name="copyright" content="J."><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="字符串stringASCIIASCII（American Standard Code for Information Interchange，美国信息交换标准代码）是基于拉丁字母的一套单字节编码系统 ASCII编码表： 数字和字符映射表，包括了英文字符和标点，用一个字节表示256种状态，用低7位128种状态就够了，0~127，都有对应的字符，包括可见字符、不可见字符、控制字符等。剩下的128种状态">
<meta property="og:type" content="article">
<meta property="og:title" content="Golang字符串">
<meta property="og:url" content="http://dycloudnavite.love/2023/02/13/Golang%E5%AD%97%E7%AC%A6%E4%B8%B2string/index.html">
<meta property="og:site_name" content="J.のblog">
<meta property="og:description" content="字符串stringASCIIASCII（American Standard Code for Information Interchange，美国信息交换标准代码）是基于拉丁字母的一套单字节编码系统 ASCII编码表： 数字和字符映射表，包括了英文字符和标点，用一个字节表示256种状态，用低7位128种状态就够了，0~127，都有对应的字符，包括可见字符、不可见字符、控制字符等。剩下的128种状态">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://dycloudnavite.love/img/golang.png">
<meta property="article:published_time" content="2023-02-13T06:32:00.000Z">
<meta property="article:modified_time" content="2023-08-30T02:51:21.116Z">
<meta property="article:author" content="J.">
<meta property="article:tag" content="云原生">
<meta property="article:tag" content="golang">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://dycloudnavite.love/img/golang.png"><link rel="shortcut icon" href="/dujie5416/img/touxiang.png"><link rel="canonical" href="http://dycloudnavite.love/2023/02/13/Golang%E5%AD%97%E7%AC%A6%E4%B8%B2string/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/dujie5416/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/dujie5416/',
  algolia: {"appId":"9GWB2VV7GN","apiKey":"dac56ec5b0b1f45281251ee59db4d637","indexName":"blog","hits":{"per_page":6},"languages":{"input_placeholder":"搜索文章","hits_empty":"找不到您查询的内容：${query}","hits_stats":"找到 ${hits} 条结果，用时 ${time} 毫秒"}},
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Golang字符串',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-08-30 10:51:21'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/xxx.css"><link rel="stylesheet" href="/css/universe.css"><link rel="stylesheet" href="/css/custom.css"><style type="text/css">#toggle-sidebar {bottom: 80px}</style><link rel="stylesheet" href="https://fastly.jsdelivr.net/gh/tzy13755126023/BLOG_SOURCE/css/function.min.css"><link rel="stylesheet" href="/css/rightMenu.css"><style type="text/css">#toggle-sidebar {left:100px}</style><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.cbd.int/hexo-butterfly-clock-anzhiyu/lib/clock.min.css" /><!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.2.0"><link rel="alternate" href="/dujie5416/atom.xml" title="J.のblog" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/dujie5416/img/touxiang.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/dujie5416/archives/"><div class="headline">文章</div><div class="length-num">73</div></a><a href="/dujie5416/tags/"><div class="headline">标签</div><div class="length-num">17</div></a><a href="/dujie5416/categories/"><div class="headline">分类</div><div class="length-num">11</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/dujie5416/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/dujie5416/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/dujie5416/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/dujie5416/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/dujie5416/img/longzhu.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/dujie5416/">J.のblog</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/dujie5416/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/dujie5416/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/dujie5416/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/dujie5416/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Golang字符串</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-02-13T06:32:00.000Z" title="发表于 2023-02-13 14:32:00">2023-02-13</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-08-30T02:51:21.116Z" title="更新于 2023-08-30 10:51:21">2023-08-30</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/dujie5416/categories/golang/">golang</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Golang字符串"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="字符串string"><a href="#字符串string" class="headerlink" title="字符串string"></a>字符串string</h1><h3 id="ASCII"><a href="#ASCII" class="headerlink" title="ASCII"></a>ASCII</h3><p>ASCII（American Standard Code for Information Interchange，美国信息交换标准代码）是基于拉丁字母的一套单字节编码系统</p>
<p><strong>ASCII编码表：</strong></p>
<p>数字和字符映射表，包括了英文字符和标点，用一个字节表示256种状态，用低7位128种状态就够了，0~127，都有对应的字符，包括可见字符、不可见字符、控制字符等。剩下的128种状态，用来扩展ASCII、欧洲字符编码表Latin-x</p>
<p><strong>中文编码表：</strong></p>
<p>数字到中文单一字符映射，至少常用的3k~5k个汉字，一个字节不够放，汉字只能多个字节存储</p>
<p>2字节，65536种状态，GB2312、GBK（包含了GB2312）、GB18030、BIG 5码</p>
<p>所有编码表都兼容单字节的ASCII表，但是如果有一篇文章需要多语言存储，如何实现？</p>
<p><strong>UNICODE:</strong></p>
<ul>
<li>多字节编码表，一张编码表解决全球多数字符对应问题</li>
<li>表示汉字多数使用两个字节</li>
<li>Go中’x’ 方式，保存int32、rune整数型，%c打印通过unicode编码表找到字符输出</li>
</ul>
<p><strong>UTF-8：</strong></p>
<ul>
<li>多字节</li>
<li>汉字转为3个字节</li>
<li>utf8mb4包含表情符号</li>
<li>字符串，字符序列，每个汉字就是utf-8编码的，也就是汉字是3个字节</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">s2 <span class="token operator">:=</span> <span class="token string">"你好"</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//</span>
<span class="token comment">//输出[228 189 160 229 165 189]</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>


<p><img src="/images/uk6XvIO7f5mrpwH2LAO7j8W_bKLuJQShRPZG0xiJTHs.png" alt="image"></p>
<table>
<thead>
<tr>
<th><strong>序号</strong></th>
<th><strong>转义字符</strong></th>
<th><strong>十进制数</strong></th>
<th><strong>说明</strong></th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>\x00</td>
<td>0</td>
<td>NUll</td>
</tr>
<tr>
<td>2</td>
<td>\x09</td>
<td>9</td>
<td>\t</td>
</tr>
<tr>
<td>3</td>
<td>\x0a</td>
<td>10</td>
<td>\n</td>
</tr>
<tr>
<td>4</td>
<td>\x0d</td>
<td>13</td>
<td>\r</td>
</tr>
<tr>
<td>5</td>
<td>\x31</td>
<td>1</td>
<td>字符1</td>
</tr>
<tr>
<td>6</td>
<td>\x41</td>
<td>65</td>
<td>字符A</td>
</tr>
<tr>
<td>7</td>
<td>\x61</td>
<td>97</td>
<td>字符a</td>
</tr>
<tr>
<td>8</td>
<td>\x30~39</td>
<td>48~57</td>
<td>字符0~9</td>
</tr>
</tbody></table>
<p>上面的1是字符1</p>
<p>UTF-8、GBK都兼容了ASCII</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token string">"a\x09b\x0ac \x31\x20\x41\x61"</span> 表示什么？
<span class="token comment">// a\tb\nc 1 Aa</span>
<span class="token char">'A'</span> <span class="token operator">></span> <span class="token char">'a'</span>   谁大？字符比较
<span class="token comment">// 41 > 61 false</span>
<span class="token string">"a"</span> <span class="token operator">></span> <span class="token string">"A"</span>   谁大？字符串比较
<span class="token comment">// 61 > 41 true</span>
<span class="token string">"AA"</span> <span class="token operator">></span> <span class="token string">"Aa"</span> 谁大？
<span class="token comment">// AA &lt; Aa  因为A对应的ASCII表为41  a为61  会一个一个字符的比较</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


<h3 id="二、字符"><a href="#二、字符" class="headerlink" title="二、字符"></a>二、字符</h3><p>本质上说，计算机中一切都是字节的，字符串也是多个字节组合而成，就是多个字节形成的有序序列。但是对于多字节编程的中文来说，用一个字节描述不了，需要多个字节表示一个字符，Go提供了rune类型。</p>
<ul>
<li>byte：兼容ASCII码的字符，是byte类型，即uint8别名，占用一个字节</li>
<li>rune：汉字等字符，unicode编码，是rune类型，即int32类型，占用4个字节</li>
<li>一个字符字面量使用单引号引起来</li>
</ul>
<p>字符串与字节序列转换</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">s1 <span class="token operator">:=</span> <span class="token string">"abc"</span>
s2 <span class="token operator">:=</span> <span class="token string">"测试"</span> <span class="token comment">// 字符串用utf-8编码，每个汉字占用3个字节</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">len</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 3, 6 字节 </span>
<span class="token comment">// 强制类型转换 string => []byte; string => []rune</span>
<span class="token comment">// 注意[]byte表示字节序列；[]rune表示rune序列</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// [97 98 99] 字符'a' 对应的ascii码十进制为97，十六进制为\x61</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">rune</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// rune int32 4字节，用unicode编码，兼容ascii码，所以值也为[97 98 99]</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// utf-8 bytes，长度为6即6个字节  // </span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">rune</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// unicode切片，长度为2，每个元素4字节 </span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%x, %x"</span><span class="token punctuation">,</span> <span class="token number">27979</span><span class="token punctuation">,</span> <span class="token number">35797</span><span class="token punctuation">)</span>  <span class="token comment">// %x打印十六进制数</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"~~~~~~~~~~~~~~~~~~~~~~~~~~~"</span><span class="token punctuation">)</span>
<span class="token comment">// []byte => string</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">string</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">&#123;</span><span class="token number">49</span><span class="token punctuation">,</span> <span class="token number">65</span><span class="token punctuation">,</span> <span class="token number">97</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment">//将对应字节码转换成string，值为1Aa</span>
<span class="token comment">// []rune => string</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">string</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">rune</span><span class="token punctuation">&#123;</span><span class="token number">27979</span><span class="token punctuation">,</span> <span class="token number">35797</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// rune查unicode码，测试</span>
<span class="token comment">// rune使用unicode，但是字符串内部使用utf-8</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>s2<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> s2<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> s2<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// 索引取到了什么？</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">string</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">&#123;</span><span class="token number">230</span><span class="token punctuation">,</span> <span class="token number">181</span><span class="token punctuation">,</span> <span class="token number">139</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 打印什么？</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li>string(一个整数)，强制转换为一个整数，相当于把整数当unicode码，取查一个字符，最后返回字符串</li>
<li>string(整数序列)，强制类型转换为一个整数序列，也是转换成字符串。</li>
</ul>
<h3 id="三、字符串"><a href="#三、字符串" class="headerlink" title="三、字符串"></a>三、字符串</h3><ul>
<li>字面常量，只读，不可变</li>
<li>线性数据结构，可以索引</li>
<li>值类型</li>
<li>utf-8编码</li>
</ul>
<h5 id="长度"><a href="#长度" class="headerlink" title="长度"></a>长度</h5><p>使用内建函数len，返回字符串占用的字节数。时间复杂度O(1)，字符串是字面常量，定义时已经知道长度，记录下来即可</p>
<h5 id="索引"><a href="#索引" class="headerlink" title="索引"></a>索引</h5><p>不支持负索引，索引范围[0,len(s)-1]</p>
<p>即使有中文，索引指的是<strong>按照<strong><strong>字节</strong></strong>的偏移量</strong></p>
<p>时间复杂度O(1)，使用索引计算该字符相对开头的偏移量即可。</p>
<p>对于顺序表，使用索引查找效率是最高的。</p>
<p>s[i]获取索引 i 处的UTF-8编码的一个字节</p>
<h5 id="遍历"><a href="#遍历" class="headerlink" title="遍历"></a>遍历</h5><p>C风格使用索引遍历，相当于字节遍历</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">	s <span class="token operator">:=</span> <span class="token string">"magedu马哥教育"</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">len</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//18个字节，中文占3个字节</span>
<span class="token comment">// C 风格使用索引遍历，相当于字节遍历，遍历出的元素类型为bytes字节</span>
	<span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token function">len</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">&#123;</span>
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%T %[1]v\n"</span><span class="token punctuation">,</span> s<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#125;</span>
<span class="token number">18</span>
<span class="token builtin">uint8</span> <span class="token number">109</span>
<span class="token builtin">uint8</span> <span class="token number">97</span>
<span class="token builtin">uint8</span> <span class="token number">103</span>
<span class="token builtin">uint8</span> <span class="token number">101</span>
<span class="token builtin">uint8</span> <span class="token number">100</span>
<span class="token builtin">uint8</span> <span class="token number">117</span>
<span class="token builtin">uint8</span> <span class="token number">233</span>
<span class="token builtin">uint8</span> <span class="token number">169</span>
<span class="token builtin">uint8</span> <span class="token number">172</span>
<span class="token builtin">uint8</span> <span class="token number">229</span>
<span class="token builtin">uint8</span> <span class="token number">147</span>
<span class="token builtin">uint8</span> <span class="token number">165</span>
<span class="token builtin">uint8</span> <span class="token number">230</span>
<span class="token builtin">uint8</span> <span class="token number">149</span>
<span class="token builtin">uint8</span> <span class="token number">153</span>
<span class="token builtin">uint8</span> <span class="token number">232</span>
<span class="token builtin">uint8</span> <span class="token number">130</span>
<span class="token builtin">uint8</span> <span class="token number">178</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>for-range遍历，遍历的元素类型为字符</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token comment">// </span>
	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> i2 <span class="token operator">:=</span> <span class="token keyword">range</span> s <span class="token punctuation">&#123;</span>
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%T %[1]v\n"</span><span class="token punctuation">,</span> i2<span class="token punctuation">,</span> i2<span class="token punctuation">)</span>
	<span class="token punctuation">&#125;</span>
<span class="token builtin">int32</span> <span class="token number">109</span>
<span class="token builtin">int32</span> <span class="token number">97</span>
<span class="token builtin">int32</span> <span class="token number">103</span>
<span class="token builtin">int32</span> <span class="token number">101</span>
<span class="token builtin">int32</span> <span class="token number">100</span>
<span class="token builtin">int32</span> <span class="token number">117</span>
<span class="token builtin">int32</span> <span class="token number">39532</span>
<span class="token builtin">int32</span> <span class="token number">21733</span>
<span class="token builtin">int32</span> <span class="token number">25945</span>
<span class="token builtin">int32</span> <span class="token number">32946</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="Strings库"><a href="#Strings库" class="headerlink" title="Strings库"></a>Strings库</h5><p>strings提供了大多数字符串操作函数，使用方便</p>
<p>注意：字符串是字面常量，不可以修改，很多操作都是返回新字符串。</p>
<h5 id="字符串拼接"><a href="#字符串拼接" class="headerlink" title="字符串拼接"></a>字符串拼接</h5><ul>
<li>Join：使用间隔符拼接字符串切片</li>
<li>Builder：多次拼接，推荐</li>
<li>fmt.Sprintf：方便快捷，推荐</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">a0 <span class="token operator">:=</span> <span class="token string">"dujiea"</span>
a1 <span class="token operator">:=</span> <span class="token string">"测试啊"</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%s %p  %s  %p\n"</span><span class="token punctuation">,</span> a0<span class="token punctuation">,</span> <span class="token operator">&amp;</span>a0<span class="token punctuation">,</span> a1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>a1<span class="token punctuation">)</span>
       <span class="token comment">// 方式一：可以相同类型可以直接相加</span>
a2 <span class="token operator">:=</span> a0 <span class="token operator">+</span> a1
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%s  %p\n"</span><span class="token punctuation">,</span> a2<span class="token punctuation">,</span> <span class="token operator">&amp;</span>a2<span class="token punctuation">)</span>
       <span class="token comment">// 方式二：join ，第一参数是一个字符串序列，第二参数是间隔符，意思是用什么间隔符将字符串序列中的元素拼接起来</span>
a3 <span class="token operator">:=</span> strings<span class="token punctuation">.</span><span class="token function">Join</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">&#123;</span>a0<span class="token punctuation">,</span> a1<span class="token punctuation">&#125;</span><span class="token punctuation">,</span> <span class="token string">"-------"</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>a3<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"--------------------------------"</span><span class="token punctuation">)</span>
       <span class="token comment">// 方式三：可以进行多次拼接，还可以指定类型拼接，比如byte，string，rune</span>
<span class="token keyword">var</span> a4 strings<span class="token punctuation">.</span>Builder
a4<span class="token punctuation">.</span><span class="token function">WriteByte</span><span class="token punctuation">(</span><span class="token number">0x41</span><span class="token punctuation">)</span>
a4<span class="token punctuation">.</span><span class="token function">WriteString</span><span class="token punctuation">(</span><span class="token string">"这是测试数据"</span><span class="token punctuation">)</span>
a4<span class="token punctuation">.</span><span class="token function">WriteRune</span><span class="token punctuation">(</span><span class="token number">27979</span><span class="token punctuation">)</span>
       <span class="token comment">// Write 可以指定字节序列</span>
a4<span class="token punctuation">.</span><span class="token function">Write</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">&#123;</span><span class="token number">65</span><span class="token punctuation">,</span> <span class="token number">66</span><span class="token punctuation">,</span> <span class="token number">67</span><span class="token punctuation">,</span> <span class="token number">68</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>a4<span class="token punctuation">.</span><span class="token function">String</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
       <span class="token comment">// 方式四：sprintf，返回一个新字符串</span>
d1 <span class="token operator">:=</span> <span class="token string">"haha"</span>
d2 <span class="token operator">:=</span> <span class="token string">"mmmm"</span>
<span class="token keyword">var</span> d3 <span class="token operator">=</span> fmt<span class="token punctuation">.</span><span class="token function">Sprintf</span><span class="token punctuation">(</span><span class="token string">"%s链接%s"</span><span class="token punctuation">,</span> d1<span class="token punctuation">,</span> d2<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>d3<span class="token punctuation">)</span>
       <span class="token comment">// 方式五：bytes.Buffer ，和strings.Builder用法一样</span>
<span class="token keyword">var</span> d4 <span class="token operator">=</span> bytes<span class="token punctuation">.</span>Buffer<span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>
d4<span class="token punctuation">.</span><span class="token function">WriteRune</span><span class="token punctuation">(</span><span class="token char">'\x61'</span><span class="token punctuation">)</span>
d4<span class="token punctuation">.</span><span class="token function">WriteString</span><span class="token punctuation">(</span><span class="token string">"这是第五种"</span><span class="token punctuation">)</span>
d4<span class="token punctuation">.</span><span class="token function">WriteByte</span><span class="token punctuation">(</span><span class="token char">'\x31'</span><span class="token punctuation">)</span>
d4<span class="token punctuation">.</span><span class="token function">Write</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">&#123;</span><span class="token number">0x41</span><span class="token punctuation">,</span> <span class="token char">'\x44'</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>d4<span class="token punctuation">.</span><span class="token function">String</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>简单拼接字符串常用+、fmt.Sprintf。如果手里正好有字符串的序列，可以考虑join，如果反复多次拼接，strings.Builder 是推荐的方式。bytes.Buffer用法同strings.Builder</p>
<h5 id="查询"><a href="#查询" class="headerlink" title="查询"></a>查询</h5><p>下面的函数都需要两个参数，第一个参数为父字符串，第二个参数为子串，子串就是需要查询的字符</p>
<ul>
<li>Index：从左至右搜索，返回子串第一次出现的字节索引位置。未找到，返回-1。子串为空，也返回0</li>
<li>LastIndex：从右至左，返回子串第一次出现的字节索引位置，未找到返回-1</li>
<li>IndexByte：从左到右搜索字节值，返回第一次出现的字节索引位置，未找到返回-1</li>
<li>IndexRune：和Index一样</li>
<li>IndexAny：从左到右搜索，找到其中一个子串就返回第一次出现的字节索引位置。</li>
<li>Contains：从左到右搜索，如果找到则返回true，没找到返回false</li>
<li>Count：从左到右搜索，返回子串出现的次数</li>
</ul>
<p>时间复杂度为O(n)，效率不高，该用则用，少用</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">q1 <span class="token operator">:=</span> <span class="token string">"www.mbai我du测a.com"</span>
<span class="token comment">// 从左到右依次查询，返回第一次出现的字节索引位置。如果未找到返回-1。 第一参数为父串，第二参数为要查询的子串</span>
<span class="token comment">// 如果查询的子串为"" 则返回0，如果找到了返回该字节的索引位置，如果没找到返回-1 ，注意：中文字符占3个字节！！！！！</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">Index</span><span class="token punctuation">(</span>q1<span class="token punctuation">,</span> <span class="token string">"测"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 输出索引下标13，因为'我'占用3个字节</span>
<span class="token comment">// 从右至左查询子串，注意：go语言没有负索引！！</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">LastIndex</span><span class="token punctuation">(</span>q1<span class="token punctuation">,</span> <span class="token string">"我"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 输出索引下标8</span>
<span class="token comment">// 与Index类似</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">IndexByte</span><span class="token punctuation">(</span>q1<span class="token punctuation">,</span> <span class="token char">'m'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// 与Index类似</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">IndexRune</span><span class="token punctuation">(</span>q1<span class="token punctuation">,</span> <span class="token number">27979</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// 从左至右搜索，找到给定的字符集字符串中任意一个字符就返回索引位置。未找到返回-1</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">IndexAny</span><span class="token punctuation">(</span>q1<span class="token punctuation">,</span> <span class="token string">"我u"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// 与Indexany功能一样，方向相反，从右至左</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">LastIndexAny</span><span class="token punctuation">(</span>q1<span class="token punctuation">,</span> <span class="token string">".m"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// 可以判断某个元素在不在给定的字符串中，返回bool值</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">Contains</span><span class="token punctuation">(</span>q1<span class="token punctuation">,</span> <span class="token string">".c"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// 返回字符出现的次数</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">Count</span><span class="token punctuation">(</span>q1<span class="token punctuation">,</span> <span class="token string">"ww"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="大小写"><a href="#大小写" class="headerlink" title="大小写"></a>大小写</h5><ul>
<li>ToLower：转换为小写</li>
<li>ToUpper：转换为大写</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">c1 <span class="token operator">:=</span> <span class="token string">"wwQeA222"</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">ToUpper</span><span class="token punctuation">(</span>c1<span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">ToLower</span><span class="token punctuation">(</span>c1<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">//输出</span>
WWQEA222
wwqea222<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="前后缀"><a href="#前后缀" class="headerlink" title="前后缀"></a>前后缀</h5><p>返回bool值</p>
<ul>
<li>HasPrefix：是否以xxx开头</li>
<li>HasSuffix：是否以xxx结尾</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">q1 <span class="token operator">:=</span> <span class="token string">"wwwww.mbai我du测a.com"</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">HasPrefix</span><span class="token punctuation">(</span>q1<span class="token punctuation">,</span> <span class="token string">"www"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">HasSuffix</span><span class="token punctuation">(</span>q1<span class="token punctuation">,</span> <span class="token string">"com"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<h5 id="移除"><a href="#移除" class="headerlink" title="移除"></a>移除</h5><ul>
<li>TrimSpace：去除字符串两端的空白字符</li>
<li>TrimPrefix、TrimSuffix：如果开头或结尾匹配，则去除。否则，返回原字符串的副本</li>
<li>TrimLeft：字符串开头的字符如果再字符集中，则全部移除，直到碰到第一个不在字符集中的字符为止。</li>
<li>TrimRight：字符串结尾的字符如果在字符集中，则全部移除，直到碰到第一个不在字符集中的字符为止。</li>
<li>Trim：字符串两头的字符如果再字符集中，则全部移除，直到左右都碰到第一个不在字符集中的字符为止</li>
<li>ReplaceAll可以将字符串中所有的’a’或其他字符删除</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">TrimSpace</span><span class="token punctuation">(</span><span class="token string">"\v\n\r \tabc\txyz\t \v\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">TrimPrefix</span><span class="token punctuation">(</span><span class="token string">"www.magedu.edu-马哥教育"</span><span class="token punctuation">,</span> <span class="token string">"www."</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">TrimSuffix</span><span class="token punctuation">(</span><span class="token string">"www.magedu.edu-马哥教育"</span><span class="token punctuation">,</span> <span class="token string">".edu"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">TrimLeft</span><span class="token punctuation">(</span><span class="token string">"abcdddeabeccc"</span><span class="token punctuation">,</span> <span class="token string">"abcd"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">TrimRight</span><span class="token punctuation">(</span><span class="token string">"abcdddeabeccc"</span><span class="token punctuation">,</span> <span class="token string">"abcd"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">Trim</span><span class="token punctuation">(</span><span class="token string">"abcdddeabeccc"</span><span class="token punctuation">,</span> <span class="token string">"abcd"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// 删除字符串中所有的字符'a'</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">ReplaceAll</span><span class="token punctuation">(</span>s2<span class="token punctuation">,</span> <span class="token string">"a"</span><span class="token punctuation">,</span> <span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


<h5 id="分隔"><a href="#分隔" class="headerlink" title="分隔"></a>分隔</h5><ul>
<li>Split：按照给定的分隔子串去分隔，返回切割后的字符串切片。<ul>
<li>切割字符串时被切掉的，不会出现在结果中，比如strings.Split(s1,’,’)  逗号不会出现在切片中</li>
</ul>
</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">s1 <span class="token operator">:=</span> <span class="token string">"abc今天efeda,好日志,好bmmha"</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">Split</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token string">","</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">//返回</span>
<span class="token punctuation">[</span>abc今天efeda 好日志 好bmmha<span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li>没有切割，也会返回一个元素的切片，元素就是被切的字符串，意思就是没切到把整个字符串转换为切片</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">s1 <span class="token operator">:=</span> <span class="token string">"abc今天efeda,好日志,好bmmha"</span>
s2 <span class="token operator">:=</span> strings<span class="token punctuation">.</span><span class="token function">Split</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token string">"fff"</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span>
<span class="token comment">// 输出， 没切到，返回切片，元素是字符串</span>
<span class="token punctuation">[</span>abc今天efeda<span class="token punctuation">,</span>好日志<span class="token punctuation">,</span>好bmmha<span class="token punctuation">]</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li>分隔字符串为空串，那么返回将被切割字符串按照每个rune字符分解后转成<strong>string</strong>存入切片返回</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">s1 <span class="token operator">:=</span> <span class="token string">"abc今天efeda,好日志,好bmmha"</span>
s3 <span class="token operator">:=</span> strings<span class="token punctuation">.</span><span class="token function">Split</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token string">""</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>s3<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%v %T"</span><span class="token punctuation">,</span> s3<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> s3<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment">// 输出</span>
<span class="token punctuation">[</span>a b c 今 天 e f e d a <span class="token punctuation">,</span> 好 日 志 <span class="token punctuation">,</span> 好 b m m h a<span class="token punctuation">]</span>
<span class="token comment">// 注意：每个元素都是string类型，并不是rune！！！！！！！！</span>
a <span class="token builtin">string</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li><code>SplitN(s,sep string, n int )[] string, n</code> 表示最终切片中有几个元素<ul>
<li>n &#x3D;&#x3D;0，返回空切片，切成0个子串</li>
</ul>
</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">s1 <span class="token operator">:=</span> <span class="token string">"abc今天efeda,好日志,好bmmha"</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">SplitN</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token string">","</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// 输出</span>
<span class="token punctuation">[</span><span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li>n &gt;0 ，返回切片元素的个数<ul>
<li>n&#x3D;&#x3D;1 ，返回一个元素切片，元素为原有字符串，相当于没有进行Split</li>
</ul>
</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">s1 <span class="token operator">:=</span> <span class="token string">"abc今天efeda,好日志,好bmmha"</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">SplitN</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token string">","</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">//输出</span>
<span class="token punctuation">[</span>abc今天efeda<span class="token punctuation">,</span>好日志<span class="token punctuation">,</span>好bmmha<span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<pre><code>  * n&gt;1，按照sep切割。返回多个元素的切片。按照sep切成的段数最多有x段，当n&lt;x时，会有部分剩余字符串未切； n==x时，字符串正好从头到尾切完，返回所有段的切片；n&gt;x时，和n==x一样。n表示切割出来的子串的上线，即至多切片里面有n个元素
</code></pre>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">s1 <span class="token operator">:=</span> <span class="token string">"abc今天efeda,好日志,好bmmha"</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">SplitN</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token string">","</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// 返回</span>
<span class="token punctuation">[</span>abc今天efeda 好日志 好bmmha<span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li>n &lt;0 ，等价Split，能切多少切多少</li>
<li>SplitAfter和Split相似，就是不把sep切掉</li>
<li>SplitAfterN和Splitn相似，也不把sep切掉</li>
<li><code>Cut(s，sep string) (before, after string, found bool)</code> <ul>
<li>内部使用index找sep，所以是从左到右搜索切割点。可以认为是切一刀，一刀两段</li>
<li>没有切到，返回s,””,false</li>
</ul>
</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">s1 <span class="token operator">:=</span> <span class="token string">"abc今天efeda,好日志,好bmmha"</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">Cut</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token string">"123"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// 输出 没切到，返回原字符串 "" false</span>
abc今天efeda<span class="token punctuation">,</span>好日志<span class="token punctuation">,</span>好bmmha  <span class="token boolean">false</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li>切到了，匹配切割符的部分要切掉，返回<code>切割符前部分，切割符后部分，true</code></li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">s1 <span class="token operator">:=</span> <span class="token string">"abc今天efeda,好日志,好bmmha"</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">Cut</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token string">","</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// 输出 前一段  后一段  true</span>
abc今天efeda 好日志<span class="token punctuation">,</span>好bmmha <span class="token boolean">true</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>


<h5 id="替换"><a href="#替换" class="headerlink" title="替换"></a>替换</h5><ul>
<li><code>Replace(s，old，new string，n int) string</code> <ul>
<li>n&lt;0，等价ReplaceAll，全部替换</li>
<li>n&#x3D;&#x3D;0，或old&#x3D;&#x3D;new，就返回原字符串，表示不替换</li>
<li>n&gt;0，至多替换n次，如果n超过找到old子串的次数x，也就只能替换x次了</li>
<li>未找到替换出，就返回原字符串</li>
</ul>
</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">s1 <span class="token operator">:=</span> <span class="token string">"abc今天efeda,好日志,好bmmha"</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">Replace</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token string">"今天"</span><span class="token punctuation">,</span> <span class="token string">"你好"</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">Replace</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token string">"好"</span><span class="token punctuation">,</span> <span class="token string">"坏"</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 指定替换1次</span>

<span class="token comment">//输出</span>
abc你好efeda<span class="token punctuation">,</span>好日志<span class="token punctuation">,</span>好bmmha
abc今天efeda<span class="token punctuation">,</span>坏日志<span class="token punctuation">,</span>好bmmha

<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


<h5 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h5><ul>
<li>Repact：使用给定的字符串重复n次拼接成一个新字符串</li>
<li>Map：按照给定处理每个rune字符的函数依次处理每个字符后，拼接成字符串返回。注意Map是一对一的映射，不能减少元素个数</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">s1 <span class="token operator">:=</span> <span class="token string">"abc今天efeda,好日志,好bmmha"</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">Repect</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 这样会把s1 拼接4次输出</span>
<span class="token comment">// 输出</span>
abc今天efeda<span class="token punctuation">,</span>好日志<span class="token punctuation">,</span>好bmmhaabc今天efeda<span class="token punctuation">,</span>好日志<span class="token punctuation">,</span>好bmmhaabc今天efeda<span class="token punctuation">,</span>好日志<span class="token punctuation">,</span>好bmmha

<span class="token comment">// 使用strings.Map 函数可以对每个rune字符进行处理然后并将他们拼接成一个字符串返回</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">Map</span><span class="token punctuation">(</span><span class="token keyword">func</span><span class="token punctuation">(</span>r <span class="token builtin">rune</span><span class="token punctuation">)</span> <span class="token builtin">rune</span> <span class="token punctuation">&#123;</span>
	<span class="token keyword">if</span> r <span class="token operator">==</span> <span class="token char">'a'</span> <span class="token punctuation">&#123;</span>
		r <span class="token operator">=</span> <span class="token char">'测'</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">return</span> r
<span class="token punctuation">&#125;</span><span class="token punctuation">,</span> s1<span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


<h3 id="四、类型转换"><a href="#四、类型转换" class="headerlink" title="四、类型转换"></a>四、类型转换</h3><h5 id="数值类型转换"><a href="#数值类型转换" class="headerlink" title="数值类型转换"></a>数值类型转换</h5><ul>
<li>低精度向高精度转换可以，高精度向低精度转换会损失精度</li>
<li>无符号向有符号转换，最高位是符号位</li>
<li>byte和int可以相互转换</li>
<li>float和int可以相互转换，float到int会丢失精度</li>
<li>bool和int不能互相转换</li>
<li>不同长度的int和float之间可以互相转换</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token comment">// 无符号和有符号之间的转换</span>
<span class="token comment">// 反码：符号位不变，按位取反</span>
<span class="token comment">// 补码：在反码基础上最末尾+1</span>
<span class="token keyword">var</span> a <span class="token builtin">int8</span> <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">10</span>
<span class="token number">1</span> <span class="token number">000</span> <span class="token number">1010</span>  <span class="token comment">// 这是有符号的二进制数，最高位为符号位，如果是负数则为1，正数则为0</span>
<span class="token number">1</span> <span class="token number">111</span> <span class="token number">0110</span>  <span class="token comment">// 按照上面的转换规则，符号位不变还是1，其他位取反，就是1 111 0101 ，然后在反码的基础上最末尾+1就成了1 111 0110 所以11110110转换为十进制就是246  </span>
<span class="token keyword">var</span> b <span class="token builtin">uint8</span> <span class="token operator">=</span> <span class="token function">uint8</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> 
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
<span class="token comment">//输出</span>
<span class="token operator">-</span><span class="token number">10</span> <span class="token number">246</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">int</span><span class="token punctuation">(</span><span class="token number">3.14</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 错误，不允许无类型float常量转到int</span>
<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token number">3.14</span> <span class="token comment">// 定义有类型变量转换就没有问题</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%T: %[1]v => %T %[2]d\n"</span><span class="token punctuation">,</span> a<span class="token punctuation">,</span> <span class="token function">int</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// float64: 3.14 => int 3</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token comment">// byte rune本质上就是整数和无类型常量可以直接计算，自动转换</span>
b <span class="token operator">:=</span> <span class="token char">'a'</span>
c <span class="token operator">:=</span> b <span class="token operator">+</span> <span class="token number">1</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%T %[1]v"</span><span class="token punctuation">,</span> c<span class="token punctuation">)</span> <span class="token comment">// 请问c显示什么，什么类型</span>
<span class="token comment">// c 显示98，rune int32类型</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>但是，如果不使用无类型常量，有类型的计算如果类型不一致要报错，因为go是对类型要求非常严苛的语言，要强制类型转换。</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">b <span class="token operator">:=</span> <span class="token char">'a'</span>
e <span class="token operator">:=</span> <span class="token number">1</span> c <span class="token operator">:=</span> b <span class="token operator">+</span> e <span class="token comment">// rune和int类型不能加，必须转换。比如c := int(b) + e或c := b + rune(e)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<h5 id="类型别名和类型定义"><a href="#类型别名和类型定义" class="headerlink" title="类型别名和类型定义"></a>类型别名和类型定义</h5><pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token keyword">var</span> a <span class="token builtin">byte</span> <span class="token operator">=</span> <span class="token char">'C'</span>
<span class="token keyword">var</span> b <span class="token builtin">uint8</span> <span class="token operator">=</span> <span class="token number">49</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> a<span class="token operator">+</span>b<span class="token punctuation">)</span> <span class="token comment">// 为什么类型不同，可以相加？// 因为byte就是uint8的别名</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p>原因是在源码中定义了 type byte &#x3D; uint8 ，byte是uint8的别名。</p>
<p>Go v1.9开始使用了类型别名，将byte等的定义修改成了类型别名的方式。</p>
<p>别名说明就是uint8的另外一个名字，和uint8是一回事。再看一段代码，正确吗？</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token keyword">type</span> myByte <span class="token builtin">uint8</span>
<span class="token keyword">var</span> a <span class="token builtin">byte</span> <span class="token operator">=</span> <span class="token char">'C'</span>
<span class="token keyword">var</span> b <span class="token builtin">uint8</span> <span class="token operator">=</span> <span class="token number">49</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> a<span class="token operator">+</span>b<span class="token punctuation">)</span> <span class="token comment">// 为什么类型不同，可以相加？byte本质上讲就是uint8</span>
<span class="token keyword">var</span> c myByte <span class="token operator">=</span> <span class="token number">50</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> c<span class="token punctuation">,</span> a <span class="token operator">+</span> c<span class="token punctuation">)</span> <span class="token comment">// 可以吗？为什么？ 不可以，因为myByte是全新类型</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>答案是不可以。原因就是Go原因不允许不同类型随便运算。就算我们眼睛看到可以，也不行，必须强制类型转换，转换是否成功，程序员自己判断</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token keyword">type</span> myByte <span class="token builtin">uint8</span> <span class="token comment">// 类型定义</span>
<span class="token keyword">type</span> <span class="token builtin">byte</span> <span class="token operator">=</span> <span class="token builtin">uint8</span> <span class="token comment">// 类型别名</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>byte只是存在于代码中，为了方便阅读或使用，编译完成后，不会有byte类型。</p>
<h5 id="字符串转换"><a href="#字符串转换" class="headerlink" title="字符串转换"></a>字符串转换</h5><pre class="line-numbers language-go" data-language="go"><code class="language-go">fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">string</span><span class="token punctuation">(</span><span class="token number">126</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// string 会把</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strconv<span class="token punctuation">.</span><span class="token function">Itoa</span><span class="token punctuation">(</span><span class="token number">126</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%T %[1]v\n"</span><span class="token punctuation">,</span> strconv<span class="token punctuation">.</span><span class="token function">Itoa</span><span class="token punctuation">(</span><span class="token number">126</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// 输出</span>
<span class="token number">126</span>
<span class="token builtin">string</span> <span class="token number">126</span>

<span class="token comment">//fmt.Println(strconv.Itoa(126))</span>
s1 <span class="token operator">:=</span> <span class="token string">"126"</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strconv<span class="token punctuation">.</span><span class="token function">Atoi</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 十进制理解126</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strconv<span class="token punctuation">.</span><span class="token function">ParseInt</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strconv<span class="token punctuation">.</span><span class="token function">ParseInt</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 相当于把126转成16进制，1*16^2 + 2*16^1 + 6*16^0 = 256+32+6 = 294</span>
<span class="token comment">// 输出</span>
<span class="token number">126</span> <span class="token operator">&lt;</span><span class="token boolean">nil</span><span class="token operator">></span>
<span class="token number">126</span> <span class="token operator">&lt;</span><span class="token boolean">nil</span><span class="token operator">></span>
<span class="token number">294</span> <span class="token operator">&lt;</span><span class="token boolean">nil</span><span class="token operator">></span>

s2 <span class="token operator">:=</span> <span class="token string">"115.6"</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strconv<span class="token punctuation">.</span><span class="token function">ParseFloat</span><span class="token punctuation">(</span>s2<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strconv<span class="token punctuation">.</span><span class="token function">ParseFloat</span><span class="token punctuation">(</span>s2<span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>strconv<span class="token punctuation">.</span><span class="token function">ParseBool</span><span class="token punctuation">(</span><span class="token string">"false"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// 输出</span>
<span class="token number">115.6</span> <span class="token operator">&lt;</span><span class="token boolean">nil</span><span class="token operator">></span>
<span class="token number">115.6</span> <span class="token operator">&lt;</span><span class="token boolean">nil</span><span class="token operator">></span>
<span class="token boolean">false</span> <span class="token operator">&lt;</span><span class="token boolean">nil</span><span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


















</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://dycloudnavite.love">J.</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://dycloudnavite.love/2023/02/13/Golang%E5%AD%97%E7%AC%A6%E4%B8%B2string/">http://dycloudnavite.love/2023/02/13/Golang%E5%AD%97%E7%AC%A6%E4%B8%B2string/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://dycloudnavite.love" target="_blank">J.のblog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/dujie5416/tags/%E4%BA%91%E5%8E%9F%E7%94%9F/">云原生</a><a class="post-meta__tags" href="/dujie5416/tags/golang/">golang</a></div><div class="post_share"><div class="addthis_inline_share_toolbox"></div><script src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5d034d84b42927bf" async="async"></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/dujie5416/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/dujie5416/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/dujie5416/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/dujie5416/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/dujie5416/2023/02/23/Django%E8%A7%86%E5%9B%BE%E7%B1%BBView%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90/"><img class="prev-cover" src="/dujie5416/img/django.png" onerror="onerror=null;src='/dujie5416/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Django视图类View源码分析</div></div></a></div><div class="next-post pull-right"><a href="/dujie5416/2023/02/12/Golang%E5%88%86%E6%94%AF%E3%80%81%E5%BE%AA%E7%8E%AF%E3%80%81%E9%9A%8F%E6%9C%BA%E6%95%B0/"><img class="next-cover" src="/dujie5416/img/golang.png" onerror="onerror=null;src='/dujie5416/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Golang分支循环随机数</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/dujie5416/2023/05/20/Channel%E9%80%9A%E9%81%93/" title="Channel通道"><img class="cover" src="/dujie5416/img/golang.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-20</div><div class="title">Channel通道</div></div></a></div><div><a href="/dujie5416/2023/05/06/Golang%E4%B8%AD%E7%9A%84GORM/" title="Golang GORM使用"><img class="cover" src="/dujie5416/img/golang.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-06</div><div class="title">Golang GORM使用</div></div></a></div><div><a href="/dujie5416/2023/06/23/Gin%E6%A1%86%E6%9E%B6/" title="Gin框架"><img class="cover" src="/dujie5416/img/golang.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-06-23</div><div class="title">Gin框架</div></div></a></div><div><a href="/dujie5416/2023/05/15/Golang%E5%8D%8F%E7%A8%8B/" title="Golang协程"><img class="cover" src="/dujie5416/img/golang.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-15</div><div class="title">Golang协程</div></div></a></div><div><a href="/dujie5416/2023/04/17/Golang%E5%8F%98%E9%87%8F%E9%80%83%E9%80%B8%E2%80%94%E2%80%94%E5%A0%86%E3%80%81%E6%A0%88/" title="Golang变量逃逸————堆、栈"><img class="cover" src="/dujie5416/img/golang.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-17</div><div class="title">Golang变量逃逸————堆、栈</div></div></a></div><div><a href="/dujie5416/2023/04/15/Golang%20List/" title="Golang List"><img class="cover" src="/dujie5416/img/golang.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-15</div><div class="title">Golang List</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="lv-container" data-id="city" data-uid="MTAyMC81Njk5OS8zMzQ2Mw=="></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/dujie5416/img/touxiang.png" onerror="this.onerror=null;this.src='/dujie5416/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">J.</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/dujie5416/archives/"><div class="headline">文章</div><div class="length-num">73</div></a><a href="/dujie5416/tags/"><div class="headline">标签</div><div class="length-num">17</div></a><a href="/dujie5416/categories/"><div class="headline">分类</div><div class="length-num">11</div></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2string"><span class="toc-number">1.</span> <span class="toc-text">字符串string</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#ASCII"><span class="toc-number">1.0.1.</span> <span class="toc-text">ASCII</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%AD%97%E7%AC%A6"><span class="toc-number">1.0.2.</span> <span class="toc-text">二、字符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">1.0.3.</span> <span class="toc-text">三、字符串</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%95%BF%E5%BA%A6"><span class="toc-number">1.0.3.0.1.</span> <span class="toc-text">长度</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%B4%A2%E5%BC%95"><span class="toc-number">1.0.3.0.2.</span> <span class="toc-text">索引</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%81%8D%E5%8E%86"><span class="toc-number">1.0.3.0.3.</span> <span class="toc-text">遍历</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Strings%E5%BA%93"><span class="toc-number">1.0.3.0.4.</span> <span class="toc-text">Strings库</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%8B%BC%E6%8E%A5"><span class="toc-number">1.0.3.0.5.</span> <span class="toc-text">字符串拼接</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2"><span class="toc-number">1.0.3.0.6.</span> <span class="toc-text">查询</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%A4%A7%E5%B0%8F%E5%86%99"><span class="toc-number">1.0.3.0.7.</span> <span class="toc-text">大小写</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%89%8D%E5%90%8E%E7%BC%80"><span class="toc-number">1.0.3.0.8.</span> <span class="toc-text">前后缀</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%A7%BB%E9%99%A4"><span class="toc-number">1.0.3.0.9.</span> <span class="toc-text">移除</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%86%E9%9A%94"><span class="toc-number">1.0.3.0.10.</span> <span class="toc-text">分隔</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%9B%BF%E6%8D%A2"><span class="toc-number">1.0.3.0.11.</span> <span class="toc-text">替换</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%85%B6%E4%BB%96"><span class="toc-number">1.0.3.0.12.</span> <span class="toc-text">其他</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.0.4.</span> <span class="toc-text">四、类型转换</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%95%B0%E5%80%BC%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.0.4.0.1.</span> <span class="toc-text">数值类型转换</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E5%88%AB%E5%90%8D%E5%92%8C%E7%B1%BB%E5%9E%8B%E5%AE%9A%E4%B9%89"><span class="toc-number">1.0.4.0.2.</span> <span class="toc-text">类型别名和类型定义</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.0.4.0.3.</span> <span class="toc-text">字符串转换</span></a></li></ol></li></ol></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/dujie5416/2023/07/29/%E6%A0%B9%E6%8D%AErouter%E6%98%BE%E7%A4%BA%E5%B7%A6%E4%BE%A7%E8%8F%9C%E5%8D%95%E6%A0%8F%E9%9A%90%E8%97%8F%E6%9F%90%E4%B8%AA%E8%8F%9C%E5%8D%95%E9%80%89%E9%A1%B9/" title="vue3左侧菜单隐藏某选项"><img src="/dujie5416/img/golang.png" onerror="this.onerror=null;this.src='/dujie5416/img/404.jpg'" alt="vue3左侧菜单隐藏某选项"/></a><div class="content"><a class="title" href="/dujie5416/2023/07/29/%E6%A0%B9%E6%8D%AErouter%E6%98%BE%E7%A4%BA%E5%B7%A6%E4%BE%A7%E8%8F%9C%E5%8D%95%E6%A0%8F%E9%9A%90%E8%97%8F%E6%9F%90%E4%B8%AA%E8%8F%9C%E5%8D%95%E9%80%89%E9%A1%B9/" title="vue3左侧菜单隐藏某选项">vue3左侧菜单隐藏某选项</a><time datetime="2023-07-29T06:32:00.000Z" title="发表于 2023-07-29 14:32:00">2023-07-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/dujie5416/2023/07/13/Vue%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86/" title="Vue状态管理"><img src="/dujie5416/img/golang.png" onerror="this.onerror=null;this.src='/dujie5416/img/404.jpg'" alt="Vue状态管理"/></a><div class="content"><a class="title" href="/dujie5416/2023/07/13/Vue%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86/" title="Vue状态管理">Vue状态管理</a><time datetime="2023-07-13T09:29:00.000Z" title="发表于 2023-07-13 17:29:00">2023-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/dujie5416/2023/06/27/%E5%9F%BA%E4%BA%8EIOC%E8%A7%A3%E5%86%B3%E5%AF%B9%E8%B1%A1%E4%BE%9D%E8%B5%96%E9%97%AE%E9%A2%98/" title="基于IOC解决对象依赖问题"><img src="/dujie5416/img/golang.png" onerror="this.onerror=null;this.src='/dujie5416/img/404.jpg'" alt="基于IOC解决对象依赖问题"/></a><div class="content"><a class="title" href="/dujie5416/2023/06/27/%E5%9F%BA%E4%BA%8EIOC%E8%A7%A3%E5%86%B3%E5%AF%B9%E8%B1%A1%E4%BE%9D%E8%B5%96%E9%97%AE%E9%A2%98/" title="基于IOC解决对象依赖问题">基于IOC解决对象依赖问题</a><time datetime="2023-06-27T03:32:00.000Z" title="发表于 2023-06-27 11:32:00">2023-06-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/dujie5416/2023/06/23/Gin%E6%A1%86%E6%9E%B6/" title="Gin框架"><img src="/dujie5416/img/golang.png" onerror="this.onerror=null;this.src='/dujie5416/img/404.jpg'" alt="Gin框架"/></a><div class="content"><a class="title" href="/dujie5416/2023/06/23/Gin%E6%A1%86%E6%9E%B6/" title="Gin框架">Gin框架</a><time datetime="2023-06-23T06:32:00.000Z" title="发表于 2023-06-23 14:32:00">2023-06-23</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/dujie5416/2023/06/04/ServeMux%E3%80%81httprouter/" title="ServeMux、httprouter"><img src="/dujie5416/img/golang.png" onerror="this.onerror=null;this.src='/dujie5416/img/404.jpg'" alt="ServeMux、httprouter"/></a><div class="content"><a class="title" href="/dujie5416/2023/06/04/ServeMux%E3%80%81httprouter/" title="ServeMux、httprouter">ServeMux、httprouter</a><time datetime="2023-06-04T03:32:00.000Z" title="发表于 2023-06-04 11:32:00">2023-06-04</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/dujie5416/img/longzhu.png')"><div id="footer-wrap"><div class="copyright">&copy;2019 - 2024  <i id="heartbeat" class="fa fas fa-heartbeat"></i> J.</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a><a href="http://beian.miit.gov.cn/"  style="color:#f72b07" target="_blank">京ICP备2022023567号</a></div></div><link rel="stylesheet" href="https://fastly.jsdelivr.net/gh/HCLonely/images@master/others/heartbeat.min.css"></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="algolia-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="search-wrap"><div id="algolia-search-input"></div><hr/><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-info"><div class="algolia-stats"></div><div class="algolia-poweredBy"></div></div></div></div></div><div id="search-mask"></div></div><div id="rightMenu"><div class="rightMenu-group rightMenu-small"><div class="rightMenu-item" id="menu-backward"><i class="fa-solid fa-arrow-left"></i></div><div class="rightMenu-item" id="menu-forward"><i class="fa-solid fa-arrow-right"></i></div><div class="rightMenu-item" id="menu-refresh"><i class="fa-solid fa-arrow-rotate-right"></i></div><div class="rightMenu-item" id="menu-home"><i class="fa-solid fa-house"></i></div></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item menu-link" href="/archives/"><i class="fa-solid fa-archive"></i><span>文章归档</span></a><a class="rightMenu-item menu-link" href="/categories/"><i class="fa-solid fa-folder-open"></i><span>文章分类</span></a><a class="rightMenu-item menu-link" href="/tags/"><i class="fa-solid fa-tags"></i><span>文章标签</span></a></div><div class="rightMenu-group rightMenu-line rightMenuNormal"><a class="rightMenu-item menu-link" id="menu-radompage" href="/random/index.html"><i class="fa-solid fa-shoe-prints"></i><span>随便逛逛</span></a><div class="rightMenu-item" id="menu-translate"><i class="fa-solid fa-earth-asia"></i><span>繁简切换</span></div><div class="rightMenu-item" id="menu-darkmode"><i class="fa-solid fa-moon"></i><span>切换模式</span></div></div></div><div id="rightmenu-mask"></div><div><script src="/dujie5416/js/utils.js"></script><script src="/dujie5416/js/main.js"></script><script src="/dujie5416/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/algoliasearch/dist/algoliasearch-lite.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instantsearch.js/dist/instantsearch.production.min.js"></script><script src="/dujie5416/js/search/algolia.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadLivere () {
  if (typeof LivereTower === 'object') {
    window.LivereTower.init()
  }
  else {
    (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
    })(document, 'script');
  }
}

if ('Livere' === 'Livere' || !false) {
  if (false) btf.loadComment(document.getElementById('lv-container'), loadLivere)
  else loadLivere()
}
else {
  function loadOtherComment () {
    loadLivere()
  }
}</script></div><script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script><script data-pjax defer src="https://npm.elemecdn.com/tzy-blog/lib/js/theme/chocolate.js"></script><canvas id="universe"></canvas><script defer src="/js/universe.js"></script><script defer data-pjax src="/js/rightMenu.js"></script><div class="aplayer no-destroy" data-id="7427714271" data-server="netease" data-type="playlist" data-fixed="true" data-mini="true" data-listFolded="false" data-order="random" data-lrctype="1" data-preload="none" data-autoplay="true" muted></div><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-show-text.min.js" data-mobile="false" data-text="I,LOVE,YOU" data-fontsize="15px" data-random="false" async="async"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["meta[property=\"og:image\"]","meta[property=\"og:title\"]","meta[property=\"og:url\"]","head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>
  function butterfly_clock_anzhiyu_injector_config(){
    var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
    var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img class="entered loading" id="card-clock-loading" src="https://cdn.cbd.int/hexo-butterfly-clock-anzhiyu/lib/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading"/></div></div></div></div></div>';
    console.log('已挂载butterfly_clock_anzhiyu')
    if(parent_div_git) {
      parent_div_git.insertAdjacentHTML("afterbegin",item_html)
    }
  }
  var elist = 'null'.split(',');
  var cpage = location.pathname;
  var epage = 'all';
  var qweather_key = '56289903f0d448dfbe8262dba0175f70';
  var gaud_map_key = '01603f2b2d08d0e947bb101e9d9b148b';
  var baidu_ak_key = 'undefined';
  var flag = 0;
  var clock_rectangle = '112.982279,28.19409';
  var clock_default_rectangle_enable = 'false';

  for (var i=0;i<elist.length;i++){
    if (cpage.includes(elist[i])){
      flag++;
    }
  }

  if ((epage ==='all')&&(flag == 0)){
    butterfly_clock_anzhiyu_injector_config();
  }
  else if (epage === cpage){
    butterfly_clock_anzhiyu_injector_config();
  }
  </script><script src="https://widget.qweather.net/simple/static/js/he-simple-common.js?v=2.0"></script><script data-pjax src="https://cdn.cbd.int/hexo-butterfly-clock-anzhiyu/lib/clock.min.js"></script><!-- hexo injector body_end end --><script src="/dujie5416/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"tagMode":false,"debug":false,"model":{"jsonPath":"/dujie5416/live2dw/assets/tororo.model.json"},"display":{"position":"right","width":150,"height":350,"hOffset":20,"vOffset":-20},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/"});</script><script async>window.onload=function(){var a=document.createElement('script'),b=document.getElementsByTagName('script')[0];a.type='text/javascript',a.async=!0,a.src='/sw-register.js?v='+Date.now(),b.parentNode.insertBefore(a,b)};</script></body></html>